import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

/**
 * 路由表
 */
const routes = [
    {
        path: "/",
        redirect: "/dashboard",
    },
    {
        path: "/login",
        component: () =>
            import(/* webpackChunkName: "login" */ "../views/Login.vue"),
    },
    {
        path: "/",
        component: () =>
            import(/* webpackChunkName: "home" */ "../views/home/Index.vue"),
        children: [
            {
                path: "dashboard",
                component: () =>
                    import(
                        /* webpackChunkName: home*/ "../views/home/Dashboard.vue"
                    ),
            },
            {
                path: "users",
                component: () =>
                    import(
                        /* webpackChunkName: home*/ "../views/home/User.vue"
                    ),
            },
            {
                path: "roles",
                component: () =>
                    import(
                        /* webpackChunkName: home*/ "../views/home/Role.vue"
                    ),
            },
        ],
    },
];

/**
 * 路由实例
 */
const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

/**
 * 路由守卫
 */
router.beforeEach((to, from, next) => {
    if (to.path === "/login" || window.sessionStorage.getItem("token")) {
        next();
    } else {
        next("/login");
    }
});

export default router;
